﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using NguyenHoa.Web.App_Code;
using NguyenHoa.Services;
using System.Data;

namespace NguyenHoa.Web.Controls
{
  public partial class AdminMainMenu : AdminBaseControl
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!Page.IsPostBack && !Page.IsCallback)
      {
        ShowMenu();
      }

      if (Page.IsPostBack)
      {
        if (Request["__EVENTTARGET"] == "AdminMainMenuChange")
        {
          string[] paras = Request["__EVENTARGUMENT"].Split('|');
          if (paras.Length >= 2)
          {
            SelectedSubMenuItem = string.Empty;
            Response.Redirect(paras[1]);
          }
        }
      }
    }

    private void ShowMenu()
    {
      DataSet listMainMenu = new MainMenuService().GetMainMenuByLanguage(CurrentLanguage.Id, MenuTypes.Admin, CurrentUser.Username);
      rptMenu.DataSource = listMainMenu;
      rptMenu.DataBind();
    }

    protected void rptMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
      if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;
      string path = Request.Url.AbsolutePath;
      string currentPage = path.Substring(path.LastIndexOf('/') + 1);
      currentPage = currentPage.Substring(0, currentPage.IndexOf(".aspx") + 5);

      DataRowView element = (DataRowView)e.Item.DataItem;
      HtmlControl lnkMenuItem = e.Item.FindControl("lnkMenuItem") as HtmlControl;
      if (lnkMenuItem != null)
      {
        lnkMenuItem.Attributes.Add("onclick", string.Format("javascript:getCurrentMainElement('{0}','{1}')"
                                  , element["Id"] != null ? element["Id"].ToString() : string.Empty
                                  , element["Link"] != null ? element["Link"].ToString() : string.Empty));
        //lnkMenuItem.Attributes["href"] = "~/Admin/" + lnkMenuItem.Attributes["href"];
        if (element["Pages"] != null && element["Pages"].ToString().Contains("," + currentPage))
        {
          lnkMenuItem.Attributes["class"] = "active";
          SelectedMenuItem = element["Id"].ToString();
          SelectedMenuItemTitle = element["Title"].ToString();
        }
      }

      HtmlControl li = e.Item.FindControl("liMenuItem") as HtmlControl;

      #region Check Permission

      //if(element.GetAttribute("PermissionList") != null 
      //  && !element.GetAttribute("PermissionList").Contains("0"))
      //{
      //  string[] permissionList = element.GetAttribute("PermissionList").Split(',');
      //  string page = string.Empty;
      //  for (int i = 0; i < permissionList.Length; i++)
      //  {
      //    int permissionId = 0;
      //    int.TryParse(permissionList[i], out permissionId);
      //    if (HasPermission(permissionId))
      //    {
      //     // page = (Page as AdminBasePage).GetNavigatePageByPermission(permissionId);
      //      break;
      //    }
      //  }
      //  if (string.IsNullOrEmpty(page))
      //  {
      //    (Page as AdminBasePage).ErrorMessage = Resources.Messages.ERR0027; 
      //    page = "~/Admin/ErrorPage.aspx";
      //  }
      //  lnk.Attributes["href"] = page;
      //}
      #endregion
    }
  }
}